<?php

class changePasswordForm extends sfForm
{
    public function configure()
    {
        $this->setWidgets(array(
        'oldpassword' => new sfWidgetFormInput(array('type' => 'password')),
        'newpassword' => new sfWidgetFormInput(array('type' => 'password')),
        'repeatnewpassword' => new sfWidgetFormInput(array('type' => 'password')),
        ));

        $this->widgetSchema->setLabels(array(
        'oldpassword'        => "Old Password",
        'newpassword'        => "New Password",
        'repeatnewpassword'    => "Re-type new password",
        ));

        $this->setValidators(array(
        'oldpassword' => new OldPasswordValidator(array(), array('invalid' => 'Password is wrong')),
        'newpassword' => new sfValidatorString(),
        'repeatnewpassword' => new sfValidatorString(),
        ));
        $this->validatorSchema['oldpassword']->setMessage('required', 'Please Fill the Old Password Field');
        $this->validatorSchema['newpassword']->setMessage('required', 'Please Fill the New Password Field');
        $this->validatorSchema['repeatnewpassword']->setMessage('required', 'Please Repeat New Password');


        $this->validatorSchema->setPostValidator(new sfValidatorSchemaCompare('newpassword', sfValidatorSchemaCompare::EQUAL, 'repeatnewpassword', array(), array('invalid' => 'The two passwords are not matching')));

        $this->widgetSchema->setNameFormat('changepassword[%s]');
    }
    public static function produceForm(){
        return new changePasswordForm();
    }
    public function formProccess(){

        $sf_user = sfContext::getInstance()->getUser();
        $sf_user->getGuardUser()->setPassword( $this->getValue('newpassword') );
        $sf_user->getGuardUser()->save();

    }


    public function getSuccessMessage(){

        return "Password is successfully changed!";

    }

}


